<template>
<div>
    <el-dialog :title="$t('chat.edit_service')" v-model="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" :append-to-body="true">
      <!--form表单-->
      <el-form size="small" ref="form" :model="form" :rules="formRules" :label-width="formLabelWidth">
        <el-form-item :label="$t('chat.user')" prop="userName">
          <el-row>
            <el-button @click="addClick" icon="Plus">{{$t('chat.select_user')}}</el-button>
            <div v-if="form.userId > 0" class="img">
              <img :src="userInfo.avatarUrl" width="100" height="100" />
              <span>{{userInfo.userId}}({{userInfo.nickName}})</span>
            </div>
          </el-row>
        </el-form-item>
        <el-form-item :label="$t('chat.service_avatar')" prop="headPortrait">
          <el-row>
            <div class="draggable-list">
              <div class="item img-select" v-if="!form.headPortrait" @click="openProductUpload()">
                <el-icon><Plus/></el-icon>
              </div>
              <div v-if="form.headPortrait" class="item" @click="openProductUpload()">
                <img :src="form.headPortrait" width="100" height="100" />
              </div>
            </div>
          </el-row>
          <!-- <div class="gray9">建议图片尺寸为750x750</div> -->
        </el-form-item>
        <el-form-item :label="$t('chat.service_nickname')" prop="nickName">
          <el-input v-model="form.nickName"></el-input>
        </el-form-item>
        <el-form-item :label="$t('chat.service_account')" prop="userName">
          <el-input v-model="form.userName"></el-input>
        </el-form-item>
        <el-form-item :label="$t('chat.service_password')" prop="password">
          <el-input v-model="form.password" :placeholder="$t('chat.input_confirm_password')" type="password" />
        </el-form-item>
        <el-form-item :label="$t('chat.confirm_password')" prop="userName">
          <el-input v-model="form.confirmPassword" :placeholder="$t('chat.input_confirm_password')" type="password" />
        </el-form-item>
        <el-form-item :label="$t('chat.sort')" prop="sort">
          <el-input v-model="form.sort" type="number"></el-input>
        </el-form-item>
        <el-form-item :label="$t('chat.status')" prop="statu">
          <el-radio-group v-model="form.statu">
            <el-radio :label="1">{{$t('chat.enabled')}}</el-radio>
            <el-radio :label="0">{{$t('chat.closed')}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">{{$t('chat.cancel_button')}}</el-button>
          <el-button type="primary" @click="onSubmit" :loading="loading">{{$t('chat.confirm_button')}}</el-button>
        </div>
      </template>
    </el-dialog>
    <!--添加-->
    <serviceSelect :is_open="open_add" @close="closeDialogFunc"></serviceSelect>
    <Upload v-if="isProductUpload" :isupload="isProductUpload" @returnImgs="returnProductImgsFunc">{{$t('chat.upload_image')}}</Upload>
  </div>
</template>

<script>
import ServiceApi from '@/api/service.js';
import serviceSelect from '@/components/service/serviceSelect.vue';
import Upload from '@/components/file/Upload.vue';
export default {
  components: {
    Upload,
    serviceSelect
  },
  data() {
    return {
      isProductUpload: false,
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      loading: false,
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否显示*/
      dialogVisible: false,
      serviceUserId: null,
      /*form表单对象*/
      form: {
        statu: 1
      },
      userInfo: {},
      /*form验证*/
      formRules: {
        userId: [{
          required: true,
          message: $t("chat.user_required"),
          trigger: 'blur'
        }],
        nickName: [{
          required: true,
          message: $t("chat.service_nickname"),
          trigger: 'blur'
        }],
        realName: [{
          required: true,
          message: $t("chat.account_required"),
          trigger: 'blur'
        }],
        statu: [{
          required: true,
          message: $t("chat.status_required"),
          trigger: 'blur'
        }]
      }
    };
  },
  created() {},
  props: ['open', 'model'],
  watch: {
    open: function (n, o) {
      if (n != o) {
        this.dialogVisible = this.open;
        this.form.serviceUserId = this.model.serviceUserId;
        if (this.form.serviceUserId) {
          this.getData(this.form.serviceUserId);
        }
      }
    }
  },
  methods: {
    openProductUpload() {
      this.isProductUpload = true;
    },
    returnProductImgsFunc(e) {
      if (e && e.length > 0) {
        this.form.headPortrait = e[0].filePath;
      }
      this.isProductUpload = false;
    },
    /*获取详情*/
    getData(serviceUserId) {
      let self = this;
      ServiceApi.serviceDetail({
        serviceUserId: serviceUserId
      }, true).then(res => {
        self.form = res.data;
        self.form.password = '';
        self.userInfo = res.data.user || {};
        self.loading = false;
      }).catch(error => {
        console.log(error);
        self.loading = false;
      });
    },
    /*编辑*/
    onSubmit() {
      let self = this;
      self.loading = true;
      let params = self.form;
      ServiceApi.editService(params, true).then(data => {
        self.loading = false;
        ElMessage({
          message: $t("chat.edit_success"),
          type: 'success'
        });
        self.dialogFormVisible(true);
      }).catch(error => {
        self.loading = false;
      });
    },
    addClick() {
      this.open_add = true;
    },
    /*关闭获取用户弹窗*/
    closeDialogFunc(e) {
      if (e && e.type != 'error') {
        this.userInfo = e.params[0];
        this.form.userId = e.params[0].userId;
        // this.form.userName = e.params[0].nickName;
      }
      this.open_add = false;
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('close', {
          type: 'success',
          openDialog: false
        });
      } else {
        this.$emit('close', {
          type: 'error',
          openDialog: false
        });
      }
    }
  }
};
</script>

